package cn.kgc.mvc.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/4
 * @Description:
 */
public class MapperUtil {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");
            sqlSessionFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static <T> T getMapper(Class<T> tClass){
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        T mapper = sqlSession.getMapper(tClass);
        return mapper;
    }

}
